<template>
  <div class="box-main" v-if="!showDetail">
    <div class="topHeaderCard">
      <a-tabs v-model:activeKey="activeKey" :bordered="false" size="small" @change="tabsChange">
        <a-tab-pane v-for="item in diseaseLite" :key="item" :tab="item" />
      </a-tabs>
    </div>
    <div class="bax-main-content">
      <div class="page-content">
        <div class="info_card">
          <a-table
            class="ant-table-striped"
            size="small"
            :loading="loading"
            :columns="columns"
            :data-source="dataSource"
            :pagination="pagination"
            @change="paginationChange"
            :row-class-name="(_record) => (_record.isRead ? 'table-striped' : null)"
          >
            <template #bodyCell="{ column, record }">
              <template v-if="column.dataIndex == 'patName'">
                {{ record.patName }} {{ record.gender }} {{ record.age }}
              </template>
              <template v-else-if="column.key === 'action'">
                <a-button type="link" @click="setRead(record)">处理</a-button>
                <a-button type="link" @click="inRemark(record)">日志</a-button>
              </template>
            </template>
          </a-table>
        </div>
      </div>
    </div>

    <a-modal v-model:open="open" :title="'处理日志'" :mask-closable="false" :width="720">
      <a-form layout="vertical" class="remarkForm">
        <a-form-item
          label="您可在下方记录本条预警消息的工作处理日志，填写的内容不对患者可见"
          name="dispatchRef"
        >
          <a-textarea
            v-model:value="remark"
            style="width: 100%"
            :rows="10"
            :maxlength="500"
            :show-count="true"
          />
        </a-form-item>
      </a-form>
      <template #footer>
        <a-button key="back" @click="open = false">取消</a-button>
        <a-button key="submit" type="primary" @click="submitRemark">保存</a-button>
      </template>
    </a-modal>
  </div>
  <PatientInfo ref="patientInfoRef" v-else @close="goBack" />
</template>
<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import PatientInfo from '@/views/examinatePatients/patientInfo.vue'
import {
  getWarningList,
  setRemark,
  readed,
  getByDetailIdByWeb,
  taskDeleteCheck
} from '@/api/warningCenter'
import { message } from 'ant-design-vue'
import { useRouter, useRoute } from 'vue-router'
import store from '@/store'

const route = useRoute()
const router = useRouter()

const showDetail = ref(false)

// const diseaseLite = ref(['服务通知', '门诊', '住院', '健康监测', '体检A', '体检B'])
const diseaseLite = ref([])
const open = ref(false)
const dispatchRef = ref()
const implementRef = ref()
const activeKey: any = ref('服务通知')
const loading = ref(false)
const remark = ref('')

const pagination = ref<any>({
  current: 1,
  total: 0,
  pageSize: 10,
  showSizeChanger: true,
  pageSizeOptions: ['10', '20', '50', '100'],
  showQuickJumper: true
})
const paginationChange = (page: any) => {
  pagination.value = { ...page }
  loading.value = true
  getDataList()
}

const columns = ref([
  {
    title: '时间',
    dataIndex: 'createTime',
    key: 'createTime',
    align: 'left'
  },
  {
    title: '患者信息',
    dataIndex: 'patName',
    key: 'patName',
    align: 'left'
  },
  {
    title: '内容',
    dataIndex: 'content',
    key: 'content',
    align: 'left'
  },
  {
    title: '操作',
    dataIndex: 'action',
    key: 'action',
    align: 'left'
  }
])
const dataSource = ref([])

const tabsChange = (e) => {
  activeKey.value = e
  loading.value = true
  pagination.value.current = 1
  getDataList()
}

const getDataList = () => {
  getWarningList({
    noticeType: activeKey.value,
    page: pagination.value.current,
    limit: pagination.value.pageSize
  })
    .then((res) => {
      loading.value = false
      pagination.value.total = res.data.total
      dataSource.value = res.data.list
    })
    .catch(() => {
      loading.value = false
    })
}

// 处理
const setRead = async (data) => {
  if (data.isRead) {
    //
  } else {
    await readed({
      id: data.id
    }).then(async (res) => {})
  }
  // console.log(data)
  /* params noticeTag  DISPATCH：派单, EXECUTE：执行, FOLLOW： 随访, MONITOR：健康监测, GREENWAY：绿通, VISIT: 就诊 CONSULT：健康咨询 */

  const extMap = data.extMap ? JSON.parse(data.extMap) : null
  switch (data.noticeTag) {
    case 'DISPATCH':
      if (data.diseaseType == 'weightLose') {
        router.push({
          path: '/specializedDiseaseWorkbench/dispatch/index'
        })
      } else {
        router.push({
          path: '/healthManaWorkbench/dispatch/index'
        })
      }
      break
    case 'EXECUTE':
      if (data.diseaseType == 'weightLose') {
        router.push({
          path: '/specializedDiseaseWorkbench/implement/index',
          query: {
            type: 'manaPlan',
            empi: data.empi,
            userId: data.userId,
            back: 1,
            backtype: data.noticeType
          }
        })
      } else {
        router.push({
          path: '/healthManaWorkbench/implement/index',
          query: {
            type: 'manaPlan',
            empi: data.empi,
            userId: data.userId,
            back: 1,
            backtype: data.noticeType
          }
        })
      }
      break
    case 'FOLLOW':
      // router.push({
      //   path: '/examinatePatients/index',
      //   query: {
      //     type: '5',
      //     empi: data.empi,
      //     back: 1,
      //     backtype: data.noticeType
      //   }
      // })
      goPage({
        type: '5',
        empi: data.empi,
        back: 1,
        backtype: data.noticeType
      })
      break
    case 'MONITOR':
      // router.push({
      //   path: '/examinatePatients/index',
      //   query: {
      //     type: '2',
      //     empi: data.empi,
      //     back: 1,
      //     backtype: data.noticeType
      //   }
      // })
      if (extMap) {
        taskDeleteCheck({
          bizId: extMap.bizId,
          bizType: extMap.bizType
        }).then((res) => {
          if (res.data) {
            goPage({
              type: '2',
              empi: data.empi,
              back: 1,
              backtype: data.noticeType,
              labelName: extMap.bizType
            })
          } else {
            message.info('该条数据已被删除，您无需再进行处理')
          }
        })
      } else {
        goPage({
          type: '2',
          empi: data.empi,
          back: 1,
          backtype: data.noticeType
        })
      }
      break
    case 'GREENWAY':
      router.push({
        path: '/greenWayOrder/index'
      })
      break
    case 'VISIT':
      // router.push({
      //   path: '/examinatePatients/index',
      //   query: {
      //     type: '4',
      //     empi: data.empi,
      //     back: 1,
      //     backtype: data.noticeType
      //   }
      // })
      goPage({
        type: '4',
        empi: data.empi,
        back: 1,
        backtype: data.noticeType
      })
      break
    case 'CONSULT':
      if (data.diseaseType == 'weightLose') {
        router.push({
          path: '/weightLossServicePack/index'
        })
      } else {
        router.push({
          path: '/healthManaConsultTwo/index'
        })
      }

      break
    case 'PLAN_NOTICE':
      // 方案执行情况
      if (extMap) {
        getByDetailIdByWeb({
          empi: data.empi,
          detailId: extMap.detailId
        }).then((planVo: any) => {
          // console.log(planVo)
          if (planVo.data) {
            if (planVo.data.pushState) {
              message.info('该任务已推送，无法修改')
            } else {
              if (data.diseaseType == 'weightLose') {
                router.push({
                  path: '/specializedDiseaseWorkbench/implement/index',
                  query: {
                    type: 'manaPlan',
                    empi: data.empi,
                    userId: data.userId,
                    back: 1,
                    backtype: data.noticeType,
                    templateType: extMap.template_type,
                    detailId: extMap.detailId,
                    diseaseType: data.diseaseType
                  }
                })
              } else {
                router.push({
                  path: '/healthManaWorkbench/implement/index',
                  query: {
                    type: 'manaPlan',
                    empi: data.empi,
                    userId: data.userId,
                    back: 1,
                    backtype: data.noticeType,
                    templateType: extMap.template_type,
                    detailId: extMap.detailId,
                    diseaseType: data.diseaseType
                  }
                })
              }
            }
          } else {
            message.info('此任务已被删除，您无需再进行处理')
          }
        })
      }
      break
    case 'DIET_PLAN_WAIT_REVIEW':
      if (data.diseaseType == 'weightLose') {
        router.push({
          path: '/specializedDiseaseWorkbench/implement/index',
          query: {
            type: 'manaPlan',
            empi: data.empi,
            userId: data.userId,
            back: 1,
            backtype: data.noticeType,
            noticeTag: data.noticeTag
          }
        })
      } else {
        router.push({
          path: '/healthManaWorkbench/implement/index',
          query: {
            type: 'manaPlan',
            empi: data.empi,
            userId: data.userId,
            back: 1,
            backtype: data.noticeType,
            noticeTag: data.noticeTag
          }
        })
      }
      break
    case 'SPORT_PLAN_WAIT_REVIEW':
      if (data.diseaseType == 'weightLose') {
        router.push({
          path: '/specializedDiseaseWorkbench/implement/index',
          query: {
            type: 'manaPlan',
            empi: data.empi,
            userId: data.userId,
            back: 1,
            backtype: data.noticeType,
            noticeTag: data.noticeTag
          }
        })
      } else {
        router.push({
          path: '/healthManaWorkbench/implement/index',
          query: {
            type: 'manaPlan',
            empi: data.empi,
            userId: data.userId,
            back: 1,
            backtype: data.noticeType,
            noticeTag: data.noticeTag
          }
        })
      }
      break
    default:
      break
  }
}

const patientInfoRef = ref()
const goPage = (data) => {
  showDetail.value = true
  nextTick(() => {
    patientInfoRef.value.initRouterInfo(data)
  })
}

const noticeData = ref()
// 备注
const inRemark = (data) => {
  open.value = true
  noticeData.value = data

  remark.value = ''
  remark.value = data.remark
}

const submitRemark = async () => {
  setRemark({
    id: noticeData.value.id,
    remark: remark.value
  }).then(async (res) => {
    if (noticeData.value.isRead) {
      //
    } else {
      await readed({
        id: noticeData.value.id
      }).then(async (res) => {})
    }
    message.success('备注成功')
    open.value = false
    loading.value = true
    getDataList()
  })
}

const goBack = () => {
  showDetail.value = false
  loading.value = true
  pagination.value.current = 1
  getDataList()
}

onMounted(() => {
  diseaseLite.value = []
  if (store.userApp.permissionList.indexOf('hp-admin:warningCenter:serviceNotification') != -1) {
    diseaseLite.value.push('服务通知')
  }
  if (store.userApp.permissionList.indexOf('hp-admin:warningCenter:outpatientService') != -1) {
    diseaseLite.value.push('门诊')
  }
  if (store.userApp.permissionList.indexOf('hp-admin:warningCenter:beinHospital') != -1) {
    diseaseLite.value.push('住院')
  }
  if (store.userApp.permissionList.indexOf('hp-admin:warningCenter:healthMonitoring') != -1) {
    diseaseLite.value.push('健康监测')
  }
  if (store.userApp.permissionList.indexOf('hp-admin:warningCenter:physicalExaminationA') != -1) {
    diseaseLite.value.push('体检A')
  }
  if (store.userApp.permissionList.indexOf('hp-admin:warningCenter:physicalExaminationB') != -1) {
    diseaseLite.value.push('体检B')
  }
  if (route.query && route.query.backtype) {
    activeKey.value = route.query.backtype
  } else {
    activeKey.value = '服务通知'
  }
  router.replace({ query: {} })
  loading.value = true
  getDataList()
})
</script>
<style lang="less" scoped>
.box-main {
  width: 100%;
  height: 100%;
  padding: 0 !important;
  overflow: hidden !important;

  .topHeaderCard {
    width: 100%;
    background-color: #fff;

    ::v-deep(.ant-tabs-nav) {
      margin: 0;
    }

    ::v-deep(.ant-tabs-nav-wrap) {
      height: 48px;
      padding: 0 20px;
      padding-top: 2px;
    }
  }

  .bax-main-content {
    width: 100%;
    height: calc(100% - 50px);
    padding: 16px;
    overflow: hidden auto;

    .page-content {
      height: 100%;

      .info_card {
        width: 100%;
        height: 100%;
        padding: 16px;
        background-color: #fff;
      }
    }
  }
}

html[theme='light'] .ant-table-striped :deep(.table-striped) td {
  background-color: #f7f8fa !important;
  opacity: 0.6;
}

html[theme='dark'] .ant-table-striped :deep(.table-striped) td {
  background-color: rgb(29, 29, 29);
}

.remarkForm .ant-form-item {
  margin-bottom: 0;
}
</style>
